<?php
class Specialty extends AppModel {
	var $name = 'Specialty';
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasAndBelongsToMany = array(
		'Sponsor' => array(
			'className' => 'Sponsor',
			'joinTable' => 'specialties_sponsors',
			'foreignKey' => 'specialty_id',
			'associationForeignKey' => 'sponsor_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'User' => array(
			'className' => 'User',
			'joinTable' => 'specialties_users',
			'foreignKey' => 'specialty_id',
			'associationForeignKey' => 'user_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);



	function EspecialidadesDeUnaSucursal($sucursal_id=0){
        //-----------------------------------------------------------
        //            Obtiene las especialidades de una sucursal dada
        //-----------------------------------------------------------
		if($sucursal_id==0){
			$sucursal_id  = $_SESSION['Sucursal']['id'];
		}


        $sql = "select specialties.id,specialties.name
                from specialties
                    left join specialties_users 
                        on specialties.id = specialties_users.specialty_id
                    left join sucursals_users 
                        on specialties_users.user_id = sucursals_users.user_id
                    left join sucursals 
                        on sucursals_users.sucursal_id = sucursals.id
                    where sucursals.id = '$sucursal_id' ";
        $x = $this->query($sql);
        $Especialidades = array();
        if($x){
            foreach($x as $z){
                $Especialidades[$z['specialties']['id']] = $z['specialties']['name'];
            }
	        unset($x); unset($z);
	        return $Especialidades;
        } else {
        	return false;
        }

	}



	function AgendasPorEspecialidad($sucursal_id=0){
        //--------------------------------------------------------------------
        //        Obtiene las agendas por especialidades de una sucursal dada
        //--------------------------------------------------------------------
		if($sucursal_id==0){
			$sucursal_id  = $_SESSION['Sucursal']['id'];
		}


        $sql = "select specialties.id,specialties.name,agendas.id
                from specialties
                    left join agendas 
                        on specialties.id = agendas.specialty_id
                    where agendas.sucursal_id = '$sucursal_id' ";
        $x = $this->query($sql);
        $Especialidades = array();
        
        if($x){
            foreach($x as $z){
                $Especialidades[$z['agendas']['id']] = $z['specialties']['name'];
            }
	        unset($x); unset($z);
	        return $Especialidades;
        } else {
        	return false;
        }

	}





}
?>